#pragma once

#include "ZSocketNetworkController.h"
#include <boost/shared_ptr.hpp>
#include <boost/unordered_map.hpp>

namespace	Z	{

//------------------------------------------------------------------------------------------------------------------------
class	Acceptor;

class	_ZNetworkApi	ServerSocketNetworkController
	:	public SocketNetworkController
{
public:
	static	const	NetworkId	all	= 0;
public:
	ServerSocketNetworkController(u_short port, size_t numConcurrentThreads);
	virtual	~ServerSocketNetworkController();
	std::string	getAddress(NetworkId kNetworkId)	const;
private:
	virtual	bool	start(MessageReceiver* pMessageReceiver);
	virtual	void	cleanup();
	virtual	bool	onCreate(AsyncSocket* pSocket);
	virtual	bool	onClose(NetworkId kNetworkId, bool graceful, bool notify);
	virtual	bool	onIoComplete(NetworkId kNetworkId, size_t bytesTransferred, OVERLAPPED* pOverlapped);
	virtual	AsyncSocket*	getSocket(NetworkId kNetworkId)	const;
	virtual	void	send(Stream* pStream, NetworkId kNetworkId);
	virtual	void	shutdown(NetworkId kNetworkId);
	void	destroySockets();

private:
	typedef	boost::shared_ptr<Acceptor>	AcceptorPtr;
	typedef boost::unordered_map<NetworkId, AsyncSocket*>	Sockets;
	AcceptorPtr	mAcceptor;
	Sockets		mSockets;
};

}